<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0
        }
        div:nth-child(1){
            height: 100px;
            background-color: black;
        }
        div:nth-child(2){
            height: 100px;
            background-color: blue;
        }
        #btn{
            height: 50px;
            background-color: red;
            font-size: 32px;
            text-align: center;
            /* position: absolute; */
          
        }
        #btn1{
            position: relative;
            height: 50px;
        }
        body{
            height: 4000px;
        }
        #p1{
            width: 100%;
            height: 0;
            background-color: yellow;
            display: none;
            position: fixed;
            top:0;
            text-align: center;
            font-size: 50px;
            
        }
    </style>
</head>
<body>
    <div></div>
    <div></div>
    <section id="bnt1">
    <section id="btn"></section>
    
</section>
<p id="p1">滑动到某指定长度后显示吸顶</p>
    <script>
        var flag=true
        onscroll=function(){
                if(scrollY>=1000){
                    if(flag==false){
                        return
                    }
                var step=0
                flag=false
                var mytimer=setInterval(function(){
                    step+=10;
                    if(step>=100){
                        clearInterval(mytimer)
                        
                        step=100
                    }
                    p1.style.display="block"
                    p1.style.height=step+"px"
                },20)               
            }else{
                p1.style.display="none"
                flag=true           
            }        
        }
    </script>
</body>
</html>